﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputManager : MonoBehaviour {
    private Camera activeCamera; // 活动摄像机
    private double lastKickTime; // 上一次鼠标抬起的时间（用来处理双击） 
    private GameObject clickedObject; //0.5秒之内被点击物体
    private Vector2 oposition1=new Vector2();
    private Vector2 oposition2=new Vector2();
    private Vector2 m_screenPos=new Vector2();//记录手指触碰的位置
    //记录两个手指的位置
    private Vector2 nposition1 = new Vector2();
    private Vector2 nposition2 = new Vector2();

    //记录手指的每帧移动距离
    private Vector2 deltaDis1 = new Vector2();
    private Vector2 deltaDis2 = new Vector2();
    private int isforward;
    void Awake () {
        lastKickTime = Time.realtimeSinceStartup;
    }
    // Use this for initialization
    void Start () {
        activeCamera = Camera.main;
        Input.multiTouchEnabled=true;//开启多点触碰
    }

    // Update is called once per frame  射线点击事件示例

    //用于判断是否放大
    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            //放大手势
            return true;
        }
        else
        {
            //缩小手势
            return false;
        }
    }
    void Update () {
        if (Input.GetMouseButtonUp (0)) {
            Ray ray = activeCamera.ScreenPointToRay (Input.mousePosition);
            RaycastHit hit; //碰撞点
            if (Physics.Raycast (ray, out hit)) //发生碰撞
            {
                if (clickedObject == null||clickedObject != hit.collider.gameObject) {
                    clickedObject = hit.collider.gameObject;
                    lastKickTime = Time.realtimeSinceStartup;
                    return;
                }else{
                    clickedObject.SendMessage("DoubleClick");
                    clickedObject = null;
                    lastKickTime = Time.realtimeSinceStartup;
                    return;
                }
            }
        }

        if (clickedObject == null) {
            return;
        } else {
            if (Time.realtimeSinceStartup - lastKickTime > 0.5) {
                clickedObject.SendMessage("Click");
                // 向电路波形图发送消息
                // 向电路U I R C 界面发送消息
                clickedObject = null;
            }
        }
        if(Input.touchCount==1)//单点触碰移动摄像机
        {
            if(Input.touches[0].phase==TouchPhase.Began)
            {
                m_screenPos=Input.touches[0].position;
            }
            if(Input.touches[0].phase==TouchPhase.Moved)
            {
                activeCamera.transform.Translate(new Vector3(Input.touches[0].deltaPosition.x*Time.deltaTime
                ,Input.touches[0].deltaPosition.y*Time.deltaTime,0));
            }
        }
        else if(Input.touchCount>1)//多点触控
        {
            for (int i = 0; i < 2; i++)
            {
                Touch touch = Input.touches[i];
                if (touch.phase == TouchPhase.Ended)
                    break;
                if (touch.phase == TouchPhase.Moved) //手指在移动
                {

                    if (i == 0)
                    {
                        nposition1 = touch.position;
                        deltaDis1 = touch.deltaPosition;
                    }
                    else
                    {
                        nposition2 = touch.position;
                        deltaDis2 = touch.deltaPosition;

                        if (isEnlarge(oposition1, oposition2, nposition1, nposition2)) //判断手势伸缩从而进行摄像机前后移动参数缩放效果
                            isforward = 1;
                        else
                            isforward = -1;
                    }
                    //记录旧的触摸位置
                    oposition1 = nposition1;
                    oposition2 = nposition2;
                }
                //移动摄像机
                activeCamera.transform.Translate(isforward*Vector3.forward * Time.deltaTime*(Mathf.Abs(deltaDis2.x+deltaDis1.x)+Mathf.Abs(deltaDis1.y+deltaDis2.y)));
            }  
        }
    }
}